package com.ykdl.member.kid.set;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;

import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.InvatationListAdapter;
import com.ykdl.member.kid.beans.ActivityDetailBean;
import com.ykdl.member.kid.beans.ActivityListBean;
import com.ykdl.member.kid.beans.InvitationBean;
import com.ykdl.member.kid.beans.OrderBean;
import com.ykdl.member.kid.beans.SystemMessageExtraContextBean;
import com.ykdl.member.kid.beans.TopicBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.set.InvitationActivity.getDetailTag;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.kid.widget.RefreshListView.OnRefreshListener;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class InvitationListActivity extends BaseScreen {
	private RefreshListView listView;
	private InvatationListAdapter adapter;
	private ActivityListBean listBean;
	private int cursor = 0;
	private int count = 20;
	private boolean isRefresh = true;
	private List<InvitationBean> data_list = new ArrayList<InvitationBean>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContent(R.layout.activity_invitation_list);
		setTopTitle("邀请有礼", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		listView = (RefreshListView) findViewById(R.id.pullToResreshView);
		adapter = new InvatationListAdapter(this);

		listView.setMoreClick(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isRefresh = false;
				getData();
			}
		});
		listView.setonRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				data_list.clear();
				isRefresh = true;
				cursor = 0;
				getData();
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				InvitationBean item = (InvitationBean) adapter
						.getItem(position - 1);
				Intent intent = new Intent(mContext, InvitationActivity.class);
				intent.putExtra(KidAction.ACTIVITY_ID, item.getActivity_id());
				startActivity(intent);
			}
		});
		getData();
	}

	private void getData() {
		// TODO Auto-generated method stub
		if (isRefresh) {
			showProgress("正在加载中,请稍等...");
		}
		String pageurl = KidConfig.ACTIVITY_LIST;
		Map<String, String> map = new HashMap<String, String>();
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request = Wxxr.getInstance().getRequest(pageurl, map,
				null);
		TaskManager.startHttpRequest(request, new getActivityListTag(),
				ActivityListBean.class);
	}

	class getActivityListTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ActivityListBean) {
				listBean = (ActivityListBean) t;
				if (listBean.getError() == null) {
					if (listBean.getInvitation_list() != null
							&& listBean.getInvitation_list().size() > 0) {
						listView.setVisibility(View.VISIBLE);
						int index = 0;
						if(data_list.size()>1){
							index = data_list.size() - 1;
						}
						for (InvitationBean obj : listBean.getInvitation_list()) {
							data_list.add(obj);
						}
						adapter.setList(data_list);
						listView.setAdapter(adapter);
						adapter.notifyDataSetChanged();
						hidErrorTip();
						if (listBean.getNext_cursor() == listBean
								.getTotal_number()) {
//							listView.setMoreButtoIsGon(true);
							listView.setHasShowAllMessage(); 
						} else {
							cursor = listBean.getNext_cursor();
							listView.setMoreButtoIsGon(false);
						}
						if (!isRefresh) {
							listView.setSelection(index);
						}
					} else {
						listView.setVisibility(View.GONE);
						showErrorTip("没有数据", "");
					}

					listView.onRefreshComplete();
					listView.setFecthMoreOk();

				} else {
					Toast.makeText(mContext, "失败," + listBean.getDesc(), 1)
							.show();
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			listView.onRefreshComplete();
			listView.setFecthMoreOk();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub

		}

	}

}
